﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class SoReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var now = DateTime.Now;
            var s = now.ToString(IEnv.DATE_FORMAT);
            var t = now.AddDays(1).ToString(IEnv.DATE_FORMAT);
            TbDate.Text = s;

            var dba = new RDBEngine();
            CalcSo(dba, user.CorpId, s, t);
            ListSo(dba, user.CorpId, s, t, 0);
        }

        protected void BtSelect_Click(object sender, EventArgs e)
        {
            var text = TbDate.Text;
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            try
            {
                var now = DateTime.Parse(text);

                var fmt = "yyyy-MM-dd";
                var dba = new RDBEngine();
                var s = now.ToString(fmt);
                var t = now.AddDays(1).ToString(fmt);

                var user = WebUtil.CurrentUser(Session);
                CalcSo(dba, user.CorpId, s, t);
                ListSo(dba, user.CorpId, s, t, 0);
            }
            catch (Exception)
            {
                return;
            }
        }

        private void CalcSo(RDBEngine dba, string corp, string dates, string datet)
        {
            var sql1 = new StringBuilder();
            sql1.Append(" SELECT");
            sql1.Append("  t.a    a,");
            sql1.Append("  t.b    b,");
            sql1.Append("  t.c    c,");
            sql1.Append("  COUNT(0)    d");
            sql1.Append(" FROM (SELECT");
            sql1.Append("        t1.").Append(DSoHeader.ID).Append("    a,");
            sql1.Append("        t2.").Append(DTerminal.NAME).Append("    b,");
            sql1.Append("        HOUR(t1.").Append(DSoHeader.UPDATE_TIME).Append(")    c");
            sql1.Append("      FROM ").Append(DSoHeader.TABLE).Append(" t1,");
            sql1.Append("        ").Append(DTerminal.TABLE).Append(" t2");
            sql1.Append("      WHERE t1.").Append(DSoHeader.CORP_ID).Append(" = '").Append(corp).Append("'");
            sql1.Append("          AND t1.").Append(DSoHeader.CHANNEL).Append(" = '").Append(CSoHeader.CHANNEL_NAME).Append("'");
            sql1.Append("          AND t1.").Append(DSoHeader.CORP_ID).Append(" = t2.").Append(DTerminal.CORP_ID);
            sql1.Append("          AND t1.").Append(DSoHeader.TERMINAL).Append(" = t2.").Append(DTerminal.CODE);
            sql1.Append("          AND t1.").Append(DSoHeader.UPDATE_TIME).Append(" >= '").Append(dates).Append("'");
            sql1.Append("          AND t1.").Append(DSoHeader.UPDATE_TIME).Append(" < '").Append(datet).Append("') t");
            sql1.Append(" GROUP BY t.b,t.c");
            sql1.Append(" ORDER BY t.b");

            var dt = dba.ExecuteSelect(sql1.ToString());

            var user = WebUtil.CurrentUser(Session);
            int cnt = dt.Rows.Count;
            if (cnt < 1)
            {
                IdJson.Text = "var data = [{name: '1号收银台',data: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}];";
                return;
            }

            StringBuilder buf = new StringBuilder("var data = [");
            int idx = 0;
            string cs = "";
            string ct;
            int hs = 0;
            int ht;

            DataRow row;
            while (idx < cnt)
            {
                row = dt.Rows[idx++];
                ct = "" + row["b"];
                if (cs != ct)
                {
                    if (cs != "")
                    {
                        while (hs++ < 24)
                        {
                            buf.Append("0,");
                        }

                        hs = 0;

                        buf.Remove(buf.Length - 1, 1).Append("]},");
                    }

                    cs = ct;
                    buf.Append("{name: '");
                    buf.Append(cs);
                    buf.Append("',data: [");
                }

                ht = int.Parse("" + row["c"]);
                while (hs++ < ht)
                {
                    buf.Append("0,");
                }
                buf.Append(row["d"]).Append(',');
            }
            while (hs++ < 24)
            {
                buf.Append("0,");
            }
            buf.Remove(buf.Length - 1, 1).Append("]}];");

            IdJson.Text = buf.ToString();
        }

        private void ListSo(RDBEngine dba, string corp, string dates, string datet, int page)
        {
            var sql = new StringBuilder();
            sql.Append("SELECT ");
            sql.Append(" a.").Append(DSoHeader.ID);
            sql.Append(",a.").Append(DSoHeader.CODE);
            sql.Append(",b.").Append(DTerminal.NAME).Append(" AS ").Append(DSoHeader.TERMINAL);
            sql.Append(",a.").Append(DSoHeader.ITEM_QTY);
            sql.Append(",a.").Append(DSoHeader.UNIT_QTY);
            sql.Append(",a.").Append(DSoHeader.ORDER_AMOUNT);
            sql.Append(",a.").Append(DSoHeader.ROUND_AMOUNT);
            sql.Append(",a.").Append(DSoHeader.UPDATE_TIME);
            sql.Append(",a.").Append(DSoHeader.CREATE_TIME);
            sql.Append(" FROM ").Append(DSoHeader.TABLE).Append(" a, ").Append(DTerminal.TABLE).Append(" b");
            sql.Append(" WHERE a.").Append(DSoHeader.CORP_ID).Append("='").Append(corp).Append("'");
            sql.Append(" AND a.").Append(DSoHeader.CORP_ID).Append("=b.").Append(DTerminal.CORP_ID);
            sql.Append(" AND a.").Append(DSoHeader.TERMINAL).Append("=b.").Append(DTerminal.CODE);
            sql.Append(" AND a.").Append(DSoHeader.UPDATE_TIME).Append(">='").Append(dates).Append("'");
            sql.Append(" AND a.").Append(DSoHeader.UPDATE_TIME).Append("<'").Append(datet).Append("'");

            var table = dba.ExecuteSelect(sql.ToString());
            int cnt = table.Rows.Count;
            //if (cnt < 1)
            //{
            //    RpVips.DataSource = table;
            //    RpVips.DataBind();
            //    return;
            //}

            // 每页行数
            int RPP = 20;
            int num = (cnt - 1) / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpVips.DataSource = pds;
            RpVips.DataBind();
        }

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            var dba = new RDBEngine();
            ListSo(dba, WebUtil.CurrentUser(Session).CorpId, HfDates.Value, HfDatet.Value, 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            var dba = new RDBEngine();
            ListSo(dba, WebUtil.CurrentUser(Session).CorpId, HfDates.Value, HfDatet.Value, max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            var dba = new RDBEngine();
            ListSo(dba, WebUtil.CurrentUser(Session).CorpId, HfDates.Value, HfDatet.Value, max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            var dba = new RDBEngine();
            ListSo(dba, WebUtil.CurrentUser(Session).CorpId, HfDates.Value, HfDatet.Value, max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            var dba = new RDBEngine();
            ListSo(dba, WebUtil.CurrentUser(Session).CorpId, HfDates.Value, HfDatet.Value, max);
        }
        #endregion
    }
}